<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        }

        .upload-container {
            background: #ffffff;
            padding: 40px;
            border-radius: 20px;
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 500px;
            text-align: center;
        }

        .upload-header {
            margin-bottom: 30px;
        }

        .upload-header h1 {
            color: #2c3e50;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .upload-header p {
            color: #7f8c8d;
            font-size: 14px;
        }

        .file-input {
            margin-bottom: 20px;
        }

        .file-input input[type="file"] {
            display: none;
        }

        .file-input label {
            display: inline-block;
            padding: 12px 20px;
            background: #3498db;
            color: white;
            border-radius: 8px;
            cursor: pointer;
            transition: background 0.3s ease;
        }

        .file-input label:hover {
            background: #2980b9;
        }

        .upload-btn {
            width: 100%;
            padding: 14px;
            background: #2ecc71;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background 0.3s ease;
        }

        .upload-btn:hover {
            background: #27ae60;
        }

        .upload-btn:disabled {
            background: #bdc3c7;
            cursor: not-allowed;
        }

        .status-message {
            margin-top: 15px;
            font-size: 14px;
            color: #7f8c8d;
        }

        .error-message {
            color: #e74c3c;
            margin-top: 15px;
            font-size: 14px;
        }
    </style>
</head>
<body>
<div class="upload-container">
    <div class="upload-header">
        <h1>文件上传</h1>
        <p>请选择要上传的文件</p>
    </div>

    <form id="uploadForm">
        <div class="file-input">
            <input type="file" id="fileInput" name="file" required>
            <label for="fileInput">选择文件</label>
            <span id="fileName"></span>
        </div>

        <button type="button" id="uploadBtn" class="upload-btn" disabled>上传文件</button>
    </form>

    <div class="status-message" id="statusMessage"></div>
    <div class="error-message" id="errorMessage"></div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const fileInput = document.getElementById('fileInput');
        const uploadBtn = document.getElementById('uploadBtn');
        const fileNameDisplay = document.getElementById('fileName');
        const statusMessage = document.getElementById('statusMessage');
        const errorMessage = document.getElementById('errorMessage');

        // 文件选择事件
        fileInput.addEventListener('change', function () {
            if (fileInput.files.length > 0) {
                fileNameDisplay.textContent = fileInput.files[0].name;
                uploadBtn.disabled = false;
            } else {
                fileNameDisplay.textContent = '';
                uploadBtn.disabled = true;
            }
        });

        // 上传按钮点击事件
        uploadBtn.addEventListener('click', function () {
            const file = fileInput.files[0];
            if (!file) {
                alert('请先选择文件');
                return;
            }

            const formData = new FormData();
            formData.append('file', file);

            // 显示上传状态
            statusMessage.textContent = '上传中...';
            errorMessage.textContent = '';

            // AJAX 上传
            const xhr = new XMLHttpRequest();
            xhr.open('POST', '../fileUpload', true);

            // 上传完成事件
            xhr.addEventListener('load', function () {
                if (xhr.status === 200) {
                    statusMessage.textContent = '上传成功！';
                    fileInput.value = ''; // 清空文件选择
                    fileNameDisplay.textContent = '';
                    uploadBtn.disabled = true;
                } else {
                    errorMessage.textContent = '上传失败：' + xhr.responseText;
                }
            });

            // 上传错误事件
            xhr.addEventListener('error', function () {
                errorMessage.textContent = '上传过程中发生错误';
            });

            xhr.send(formData);
        });
    });
</script>
</body>
</html>